package com.zhaolulu.controller;

import cn.elegent.idem.annotation.ElegentIdem;
import cn.elegent.pay.ElegentPay;
import cn.elegent.pay.dto.PayRequest;
import cn.elegent.pay.dto.PayResponse;
import com.zhaolulu.domain.Order;
import com.zhaolulu.domain.dto.PayDto;
import com.zhaolulu.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ElegentPay elegentPay;

    @ElegentIdem(type="sn" ,name="requestId")
    @PostMapping("/requestPay/{tradeType}/{platform}")
    public PayResponse requestPay(@RequestBody PayDto payDto, @PathVariable("tradeType") String tradeType, @PathVariable("platform") String platform) {
        //1. 创建订单
        Order order = orderService.createOrder(payDto, platform);//创建订单
        //2. 调用支付
        PayRequest param=new PayRequest();
        param.setBody(order.getSkuName());//商品名称
        param.setOrderSn(order.getOrderNo());//订单编号
        param.setTotalFee(order.getAmount().intValue());//订单金额
        param.setOpenid(order.getOpenId());//openid
        return elegentPay.requestPay(param, tradeType, platform);
    }
}
